/*
 * Created on 2005-1-25
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.nauu.atombuilder.modle;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.ui.views.properties.ComboBoxPropertyDescriptor;
import org.eclipse.ui.views.properties.IPropertyDescriptor;
import org.eclipse.ui.views.properties.PropertyDescriptor;
import org.eclipse.ui.views.properties.TextPropertyDescriptor;

/**
 * @author nauu
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Connection extends Element {
	
	private static final long serialVersionUID = 1L;

	public static final int TRACK = Graphics.LINE_DASH;
	
	public static final int GRID = Graphics.LINE_CUSTOM;

	public static final int CLIP = Graphics.LINE_DOT;

	public static final String PROP_BENDPOINT = "PROP_BENDPOINT";

	public static final String CHANGE_STYLE = "CHANGE_STYLE";

	public static final String CHANGE_COLOR = "CHANGE_COLOR";

	public static final String CHANGE_LABEL = "CHANGE_LABEL";

	public static final String CHANGE_ACT_NAME = "CHANGE_ACT_NAME";

	public static final String CHANGE_ACT_TYPE = "CHANGE_ACT_TYPE";

	public static final String CHANGE_MODE_TYPE = "CHANGE_MODE_TYPE";

	private static final String ACT_NAME = "ACT_NAME";

	private static final String ACT_TYPE = "ACT_TYPE";

	private static final String MODE_TYPE = "MODE_TYPE";

	private List bendpoints = new ArrayList();
	
	private Node source;

    private Node target;
    
    private int lineStyle;

    private int actType;

    private int modeType;
    
    private String lineColor;
    
    private String label;

    private String actName = "操作按钮";
    
    protected static IPropertyDescriptor[] descriptors;

    public void setSource(Node source) {
        this.source = source;
    }

    public void setTarget(Node target) {
        this.target = target;
    }

    public Node getTarget() {
        return this.target;
    }

    public Node getSource() {
        return this.source;
    }

    public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		if (this.label == label) {
            return;
        }
        this.label = label;
        firePropertyChange(CHANGE_LABEL, null, label);
	}

	public String getLineColor() {
		return lineColor;
	}

	public void setLineColor(String lineColor) {
		if (this.lineColor == lineColor) {
            return;
        }
        this.lineColor = lineColor;
        firePropertyChange(CHANGE_COLOR, null, lineColor);
	}

	public int getLineStyle() {
		return lineStyle;
	}

	public void setLineStyle(int lineStyle) {
		if (this.lineStyle == lineStyle) {
            return;
        }
		this.lineStyle = lineStyle;
        firePropertyChange(CHANGE_STYLE, null, lineStyle);
	}

	/**
     * @param source
     * @param target
     */
    public Connection(Node source, Node target) {
        super();
        this.source = source;
        this.target = target;
        source.addOutput(this);
        target.addInput(this);
    }

	@Override
	public Object getEditableValue() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public IPropertyDescriptor[] getPropertyDescriptors() {
		if(descriptors == null){
			PropertyDescriptor type = new PropertyDescriptor(MODE_TYPE, "动画类型");
			
			ComboBoxPropertyDescriptor actType = new ComboBoxPropertyDescriptor(ACT_TYPE, "按钮类型", new String[] { "track", "clip" ,"grid" });
			
			TextPropertyDescriptor actName = new TextPropertyDescriptor(ACT_NAME, "按钮名称");
			
			descriptors = new IPropertyDescriptor[]{
					type,
					actType,
					actName
			};
		}else{
			if(descriptors.length == 0)
        		descriptors = new IPropertyDescriptor[]{};
		}
		return descriptors;
	}

	@Override
	public Object getPropertyValue(Object id) {
		if(ACT_NAME.equals(id))
			return getActName();
		if(ACT_TYPE.equals(id)){
//			switch ((Integer)getActType()) {
//			case 0:
//				return "track";
//			case 1:
//				return "clip";
//			case 2:
//				return "grid";
//			default:
//				break;
//			}
			return getActType();
		}
		if(MODE_TYPE.equals(id))
			return getModeType()==0?"loop":"pop";
		
        return null;
	}

	@Override
	public boolean isPropertySet(Object arg0) {
		return true;
	}

	@Override
	public void resetPropertyValue(Object arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setPropertyValue(Object id, Object value) {
		if(ACT_NAME.equals(id))
            setActName((String)value);
		if(ACT_TYPE.equals(id))
			setActType((Integer)value);
		if(MODE_TYPE.equals(id))
			setModeType((Integer)value);
	}

	public String getActName() {
		return actName;
	}

	public void setActName(String actName) {
		if (this.actName == actName) {
            return;
        }
		this.actName = actName;
        firePropertyChange(CHANGE_ACT_NAME, null, actName);
	}

	
	public int getModeType() {
		return modeType;
	}

	public void setModeType(int modeType) {
		if (this.modeType == modeType) {
            return;
        }
		this.modeType = modeType;
        firePropertyChange(CHANGE_MODE_TYPE, null, modeType);
	}

	public int getActType() {
		return actType;
	}

	public void setActType(int actType) {
		if (this.actType == actType) {
            return;
        }
		this.actType = actType;
        firePropertyChange(CHANGE_ACT_TYPE, null, actType);
	}

	public void addBendpoint(int index, ConnectionBendpoint point) {
	    getBendpoints().add(index, point);
	    firePropertyChange(PROP_BENDPOINT, null, null);
	}

	/**
	 * zhanghao: 为了在更新两个dimension后能发送事件，在MoveBendpointCommand要在用这个方法设置新坐标，
	 * 而不是直接用BendPoint里的方法。
	 */
	public void setBendpointRelativeDimensions(int index, Dimension d1, Dimension d2){
	    ConnectionBendpoint cbp=(ConnectionBendpoint)getBendpoints().get(index);
	    cbp.setRelativeDimensions(d1,d2);
	    firePropertyChange(PROP_BENDPOINT, null, null);
	}

	public void removeBendpoint(int index) {
	    getBendpoints().remove(index);
	    firePropertyChange(PROP_BENDPOINT, null, null);
	}
	
	public List getBendpoints() {
		if(bendpoints == null){
			bendpoints = new ArrayList();
		}
		return bendpoints;
	}

	public void setBendpoints(List bendpoints) {
		this.bendpoints = bendpoints;
	}
    
	
	
}